//
//  ReportDetailVC.m
//  Cunli
//
//  Created by lilubin on 15/6/25.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ReportDetailVC.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "MBProgressHUD.h"

#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
#define kUIScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ReportDetailVC () <UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSArray *_reportReasons; //存储举报原因字符串
    NSUInteger _reportType;
}

@property (nonatomic, strong) UIPickerView *reportTypePickerView;

@end

@implementation ReportDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAllGlobalParams];
    [self setAllUIKitAttributes];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initAllGlobalParams
{
    _reportReasons = [NSArray arrayWithObjects:@"色情", @"谣言", @"钓鱼链接", @"诱导分析", nil];
    _reportType = 0;
}

- (void)setAllUIKitAttributes
{
    
    self.title = @"举报";
    
    [_additionTextfield.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [_additionTextfield.layer setBorderWidth:1.0];
    [_additionTextfield.layer setCornerRadius:7];
    
    _reportTypePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, kUIScreenHeight - 200, kUIScreenWidth-40, 280)];
    _reportTypePickerView.delegate = self;
    _reportTypePickerView.dataSource = self;
    _reportTypePickerView.backgroundColor = [UIColor clearColor];
    
    _reasonTextfield.inputView = _reportTypePickerView;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
    [rightBtn setTitle:@"提交" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    if (_reportType == 0) {
        [Config showHUDWithText:@"举报原因不能为空" andImage:@"error"];
        return;
    }
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:6];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:self.itemid forKey:@"itemid"];
    [CheckParams setObject:self.m_itemid forKey:@"m_itemid"];
    
    [CheckParams setObject:[NSString stringWithFormat:@"%lu", (unsigned long)_reportType] forKey:@"type"];
    [CheckParams setObject:_additionTextfield.text forKey:@"content"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"report.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        NSString *response = result[@"response"];
        if ([response isEqualToString:@"report"]) {
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"举报成功";
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *errorStr = [errorDic objectForKey:@"text"];
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.detailsLabelText = errorStr;
        }
        [hud hide:YES afterDelay:1];
        
    } failureAction:^{
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
        hud.labelText = @"网络错误";
        [hud hide:YES afterDelay:1];
    }];
    
}

#pragma mark -  UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _reportReasons.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _reportType = row+1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    _reasonTextfield.text = _reportReasons[row];
    _reportType = row+1;
    return _reportReasons[row];
}


@end
